//
//  WMH_User_Register_ViewController.m
//  wmh.ios.v5
//
//  Created by AndLi on 14-8-29.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "WMH_User_Register_ViewController.h"

@interface WMH_User_Register_ViewController ()

@end

@implementation WMH_User_Register_ViewController
{
    UIColor *lightcolor;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"用户注册";

    lightcolor = [[UIColor alloc]
                  
                  initWithRed:(float)244.0/256.0 green:(float)244.0/256.0 blue:(float)244.0/256.0 alpha:(float)1.0];
    [self initview];
    
    [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
}

-(void) initview{
//    _register_view.layer.cornerRadius = 5;
//    _register_view.layer.masksToBounds = YES;
//    
//    //设置边框及边框颜色
//    _register_view.layer.borderWidth = 2;
//    _register_view.layer.borderColor =[ lightcolor CGColor];
    
    [_tf_psw setSecureTextEntry:YES];
    [_tf_confirmpsw setSecureTextEntry:YES];
    
    
    //设置scrollview
    _scrollview.frame = CGRectMake(0, 0, 320, 460); //ScrollView的大小，retina屏1个点2个像素
    [_scrollview setContentSize:CGSizeMake(320, 548+180)]; //设定scrollview高度，如果内容多余一个屏幕，这里设定实际高度占多少个点。
    
   
    
    
}


//键盘退出
-(void) returnjianpan{
    [_tf_confirmpsw resignFirstResponder];
    [_tf_psw resignFirstResponder];
    [_tf_username resignFirstResponder];
    [_tf_nichen resignFirstResponder];
    [_tf_mail resignFirstResponder];
    [_tf_phone resignFirstResponder];

}

-(void)reguser:(id)sender{
    [self returnjianpan];
    NSString *name = [_tf_username text];
    NSString *psw = [_tf_psw text];
    NSString *nichen = [_tf_nichen text];
    NSString *mail = [_tf_mail text];
    NSString *phone = [_tf_phone text];
    NSString *address = [_tf_address text];
    
    if([self checkDate]){
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:name forKey:@"loginid"];
        [params setObject:nichen forKey:@"name"];
        [params setObject:[MDUtil authPassword:psw] forKey:@"password"];
        [params setObject:phone forKey:@"phone"];
        [params setObject:mail forKey:@"email"];
        [params setObject:address forKey:@"address"];
        [self startTask:@"Task_UserRegister" withParams:params withTaskId:1];
    }
    
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj{
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];

        return;
    }
    
    if (taskId==1) {
        NSString *bs= [arr objectAtIndex:2];
        if([bs rangeOfString:@"True"].location!=NSNotFound){
            [self toastMessage:@"注册成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self toastMessage:@"注册失败!"];
        }
    }
    
}


-(void)textFiledReturnEditing:(id)sender{
    [sender resignFirstResponder];
}


//数据校验
-(BOOL) checkDate{
    
    NSString *loginid= [_tf_username text];
    NSString *nichen = [_tf_nichen text];
    NSString *psw = [_tf_psw text];
    NSString *email = [_tf_mail text];
    NSString *phone = [_tf_phone text] ;
    
    NSString *password2 = [_tf_confirmpsw text];
    
    NSString *regex = @"^[a-zA-Z]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    //NSLog(@"%@",[loginid substringToIndex:1]);
    if([loginid isEqualToString:@""]){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"账户名不能为空！"];
        return false;
    }else if ([predicate evaluateWithObject:[loginid substringToIndex:1]] == NO) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"账户名必须以字母开头！"];
        return false;
    }else if([loginid length]<=3){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"账户名长度小于4！"];
        return false;
    }
    else if([loginid length]>16){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"账户名不能大于16！"];
        return false;
    }
    /***************** 校验昵称 *******************/
    else if ([nichen isEqualToString:@""]){
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"请输入昵称"];
        return false;
    }else if([nichen length]<=3) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"昵称长度小于4！"];
        return false;
    }
    else if([nichen length]>16) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"昵称长度不能大于16！"];
        return false;
    }
    /***************** 校验密码 *******************/
    else if ([psw isEqualToString:@""]) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"密码不能为空！"];
        return false;
    } else if ([psw length] <= 5) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"密码长度小于6！"];
        return false;
    }
    /***************** 校验2次密码 *******************/
    else if (![psw isEqualToString:password2]) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"两次密码输入的不一致，请检查！"];
        return false;
    }
    /***************** 校验邮箱，手机 *******************/
    else if ([email isEqualToString:@""]) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"邮箱不能为空！"];
        return false;
    }
    else if ([phone isEqualToString:@""]) {
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"手机号码不能为空！"];
        return false;
    }
   
    if (![email isEqualToString:@""]) {
        NSString *regex = @"^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        if([predicate1 evaluateWithObject:email] == NO){
            [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"邮箱格式不正确！"];
            return false;
        }
    }
    
    if (![phone isEqualToString:@""]) {
        
        NSString *regex = @"1[0-9]{10}";
        NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        if([predicate2 evaluateWithObject:phone] == NO){
            [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"手机号码格式不正确！"];
            return false;
        }
    }
    
    return true;
    
}
-(IBAction)checkPhoneLength:(id)sender{
    if (_tf_phone.text.length>11) {
        _tf_phone.text = [_tf_phone.text substringToIndex:11];
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"手机号码长度不能大于11！"];
    }
}

-(IBAction)checkNiChenLength:(id)sender{
    if (_tf_nichen.text.length>16) {
        _tf_nichen.text = [_tf_nichen.text substringToIndex:16];
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"昵称长度不能大于16！"];
    }
}

-(IBAction)checkNameLength:(id)sender{
    if (_tf_username.text.length>16) {
        _tf_username.text = [_tf_username.text substringToIndex:16];
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"用户名长度不能大于16！"];
    }
}
-(IBAction)checkPassWordLength:(id)sender{
    if (_tf_psw.text.length>16) {
        _tf_psw.text = [_tf_psw.text substringToIndex:16];
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"密码长度不能大于16！"];
    }
}
-(IBAction)checkPassWord2Length:(id)sender{
    if (_tf_confirmpsw.text.length>16) {
        _tf_confirmpsw.text = [_tf_confirmpsw.text substringToIndex:16];
        [AlertUtil showAlertDialogWithSingleButtonNoAction:@"确定" info:@"密码长度不能大于16！"];
    }
}

@end

